package com.cardone.platform.common.dao;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import lombok.Getter;
import lombok.Setter;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.util.EntityUtils;
import com.cardone.context.Attributes;
import com.cardone.context.ContextHolder;
import com.cardone.persistent.builder.Model;
import com.cardone.persistent.builder.ModelArgs;
import com.cardone.persistent.builder.ModelUtils;
import com.cardone.persistent.support.JdbcTemplateSupport;
import com.cardone.persistent.support.PaginationArgs;
import com.cardone.platform.common.dto.EntityLogDto;
import com.google.common.collect.Maps;

/**
 * 实体日志
 *
 * @author yaohaitao
 *
 */
@Getter
@Setter
public class EntityLogJdbcDao implements EntityLogDao {
	@Override
	public int deleteByIds(final EntityLogDto deleteEntityLog) {
		final Model model = ModelUtils.newModel();

		if (MapUtils.isNotEmpty(deleteEntityLog.getAttrs())) {
			model.putAll(deleteEntityLog.getAttrs());
		}

		model.put(Attributes.ids.name(), deleteEntityLog.getIds());

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.deleteByIds.id(), model);
	}

	@Override
	public <P> P findByCodeOrId(final Class<P> mappedClass, final EntityLogDto findEntityLog) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findEntityLog, Attributes.code.name());

		final Model model = ModelUtils.newModel(new ModelArgs(findEntityLog, whereAndEqProperties));

		if (MapUtils.isNotEmpty(findEntityLog.getAttrs())) {
			model.putAll(findEntityLog.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findByCode.id(), model);
	}

	@Override
	public <P> P findById(final Class<P> mappedClass, final EntityLogDto findEntityLog) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findEntityLog);

		final Model model = ModelUtils.newModel(new ModelArgs(findEntityLog, whereAndEqProperties).setIsSimple(true));

		if (MapUtils.isNotEmpty(findEntityLog.getAttrs())) {
			model.putAll(findEntityLog.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findById.id(), model);
	}

	@Override
	public <P> List<P> findListByLikeCode(final Class<P> mappedClass, final EntityLogDto findListEntityLog) {
		final Model model = ModelUtils.newModel(new ModelArgs(findListEntityLog).setIsSimple(true));

		if (MapUtils.isNotEmpty(findListEntityLog.getAttrs())) {
			model.putAll(findListEntityLog.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByLikeCode.id(), model);
	}

	@Override
	public int insertByCode(final EntityLogDto insertEntityLog) {
		if (StringUtils.isBlank(insertEntityLog.getId())) {
			insertEntityLog.setId(UUID.randomUUID().toString());
		}

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.insert.stringValue(), insertEntityLog));

		if (MapUtils.isNotEmpty(insertEntityLog.getAttrs())) {
			model.putAll(insertEntityLog.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.insertByCode.id(), model);
	}

	@Override
	public <P> PaginationDto<P> paginationByLikeCode(final Class<P> mappedClass, final EntityLogDto paginationEntityLog) {
		final Model model = ModelUtils.newModel(new ModelArgs(paginationEntityLog).setIsSimple(true));

		if (MapUtils.isNotEmpty(paginationEntityLog.getAttrs())) {
			model.putAll(paginationEntityLog.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).pagination(mappedClass, new PaginationArgs(SqlIds.readByLikeCode.id(), SqlIds.findByLikeCode.id(), paginationEntityLog), model);
	}

	@Override
	public Integer readByCodeNqIdForCount(final EntityLogDto readEntityLog) {
		final String[] whereAndEqProperties = { Attributes.code.name() };

		final Model model = ModelUtils.newModel(new ModelArgs(readEntityLog, whereAndEqProperties));

		if (MapUtils.isNotEmpty(readEntityLog.getAttrs())) {
			model.putAll(readEntityLog.getAttrs());
		}

		final String[] whereAndNqProperties = EntityUtils.getWhereProperties(readEntityLog);

		if (ArrayUtils.isNotEmpty(whereAndNqProperties)) {
			ModelUtils.put(model, new ModelArgs(Model.Keys.whereAndNq.stringValue(), readEntityLog, whereAndNqProperties));
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).read(Integer.class, SqlIds.readByCode.id(), model);
	}

	@Override
	public String readEntityIdByIdForValid(final String id) {
		final Map<String, Object> model = Maps.newHashMap();

		model.put(Attributes.id.name(), id);

		return ContextHolder.getBean(JdbcTemplateSupport.class).read(String.class, SqlIds.readEntityIdByIdForValid.id(), model);
	}

	@Override
	public int saveByEntityIdAndTypeId(final EntityLogDto saveEntityLog) {
		// int saveCount = this.updateByEntityIdAndTypeIdForInvalid(saveEntityLog);

		final int saveCount = this.insertByCode(saveEntityLog);

		return saveCount;
	}

	@Override
	public <P> P saveByIdOrCode(final Class<P> mappedClass, final EntityLogDto saveEntityLog) {
		final P oldEntityLog = this.findByCodeOrId(mappedClass, saveEntityLog);

		if (oldEntityLog == null) {
			this.insertByCode(saveEntityLog);

			return this.findByCodeOrId(mappedClass, saveEntityLog);
		}

		final int updateCount = this.updateByCode(saveEntityLog, oldEntityLog);

		if (updateCount < 1) {
			return oldEntityLog;
		}

		return this.findByCodeOrId(mappedClass, saveEntityLog);
	}

	@Override
	public int updateByCode(final EntityLogDto updateEntityLog) {
		final EntityLogDto oldEntityLog = this.findByCodeOrId(EntityLogDto.class, updateEntityLog);

		return this.updateByCode(updateEntityLog, oldEntityLog);
	}

	private <P> int updateByCode(final EntityLogDto updateEntityLog, final P oldEntityLog) {
		if (oldEntityLog == null) {
			return 0;
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { Attributes.parentId.name(), Attributes.typeId.name(), Attributes.code.name(), Attributes.name.name(), Attributes.value.name(), Attributes.remark.name() }, updateEntityLog);

		final String[] updateProperties = com.cardone.common.util.BeanUtils.diffProperties(updateEntityLog, oldEntityLog, useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return 0;
		}

		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(oldEntityLog, Attributes.id.name());

		com.cardone.common.util.BeanUtils.copyProperties(oldEntityLog, updateEntityLog, whereAndEqProperties);

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.update.stringValue(), updateEntityLog, updateProperties), new ModelArgs(updateEntityLog, whereAndEqProperties));

		if (MapUtils.isNotEmpty(updateEntityLog.getAttrs())) {
			model.putAll(updateEntityLog.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.updateByCode.id(), model);
	}

	@Override
	public int updateByEntityIdAndTypeIdForInvalid(final EntityLogDto updateEntityLog) {
		final Model model = ModelUtils.newModel(new ModelArgs(updateEntityLog).setIsSimple(true));

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.updateByEntityIdAndTypeIdForInvalid.id(), model);
	}
}